/* when the page has finished loading.. execute the follow */
$(document).ready(function () {
    jQuery("#companyList").jqGrid({
        url:'listAsJSON',
        datatype: "json",
        colNames:['Name', 'Street', 'City', 'State', 'Zip', 'Phone', 'Address', 'id'],
        colModel:[
            {name:'name', width:250},
            {name:'line1'},
            {name:'city'},
            {name:'state', width:45},
            {name:'zip', width:50},
            {name:'phone', width:100},
            {name:'line1', width:305},
            {name:'id', hidden:true}
        ],
        altRows: true,
        autowidth: true,
        height: 332,
        message: 'The data is not available. Please refresh the screen.',
        pager: '#companyListPager',
        rowList: [20, 50, 75, 100],
        rowNum: 20,
//        scroll: true,
        shrinkToFit: false,
        subGrid: true,
        subGridModel: [{ name : ['Title', 'First Name', 'Last Name', 'Phone', 'Email', 'Notes'],
                         width : [35, 55, 55, 40, 55, 100] }],
        subGridUrl: 'listPeopleForCompanyListAsJSON',
        viewrecords: true,
        onSelectRow: function(id) {
            window.location = "edit/" + id
        }
    });
    jQuery("#companyList").jqGrid('gridResize',{minWidth:850,minHeight:80, maxHeight:600});
});

var timeoutHnd;

function doSearch(ev) {
    if (timeoutHnd) {
        clearTimeout(timeoutHnd)
    }
    timeoutHnd = setTimeout(gridReload,500)
}

function gridReload() {
    var searchValue = jQuery("#companySearch").val();
    jQuery("#companyList").jqGrid('setGridParam', {url:"listAsJSON?searchValue=" + searchValue, page:1}).trigger("reloadGrid");
}
